/*
* Copyright (c) 2007 by Kirill Kolodyazhniy.
* See the file "license.terms" for information on usage and redistribution.
*/

#include "physicsystem.h"

namespace Engine
{
	/***************************************************************************************************************/
	PhysicSystem::~PhysicSystem()
	{
		DeleteAll();
	}
	/***************************************************************************************************************/
	SpacePtr PhysicSystem::CreateSpace(const std::string& name)
	{
		Space* space = CreateSpaceImpl(name);
		m_spaces[name] = RefPtr<Space, WeakPtrExtension>(space);
		return m_spaces[name];
	}
	/***************************************************************************************************************/
	SolidPtr PhysicSystem::CreateSolid(const std::string& name)
	{
		Solid* solid = CreateSolidImpl(name);
		m_solids[name] = RefPtr<Solid, WeakPtrExtension>(solid);
		return m_solids[name];
	}
	/***************************************************************************************************************/
	void PhysicSystem::DeleteAll()
	{
		m_solids.clear();
		m_spaces.clear();
	}
	/***************************************************************************************************************/
	void PhysicSystem::Update()
	{
		if(m_step == 0)
			return;

		// Apply forces/torques to Solids.
		SolidMap::iterator solidIter = m_solids.begin(), solidEnd = m_solids.end();
		for (;solidIter != solidEnd; ++solidIter)
			solidIter->second->ApplyForces(m_step);

		Simulate(m_step);

	}
	/***************************************************************************************************************/
	void PhysicSystem::SetStepSize(REAL step)
	{
		m_step = step;
	}
	/***************************************************************************************************************/
	void PhysicSystem::SetRootSceneNode(SceneNodePtr node)
	{
		m_rootNode = node;
	}
	/***************************************************************************************************************/
}
